package com.app.service.impl;

import com.app.mapper.TestsMapper;
import com.app.model.TestsModel;
import com.app.service.TestsService;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.ListUtils;
import org.thymeleaf.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Primary
public class TestsServiceImpl implements TestsService {
    @Autowired
    TestsMapper testsMapper;


    @Override
    public TestsModel getTestById(int id) {
        QueryWrapper<TestsModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "department_id", "name", "test_type", "year", "model_type", "subject", "start_time", "end_time", "conditions", "results", "breakdowns", "breakdowns_loc", "solutions", "review_start_time", "review_end_time", "period", "remain_cases", "advises").eq("id", id);
        TestsModel testsModel = testsMapper.selectOne(queryWrapper);
        return testsModel;
    }

    @Override
    public TestsModel selectTestById(int id) {
        return testsMapper.selectById(id);
    }

    @Override
    public Page<TestsModel> getPageTestsList(Integer page, Integer limit, String name, Integer modelType, String breakdowns, List<Integer> idList, Integer year, Integer departmentId) {
        QueryWrapper<TestsModel> queryWrapper = new QueryWrapper<>();
        if (idList.size() > 0) {
            for (int i = 0; i < idList.size(); i++) {
                queryWrapper.select().in("department_id", idList);
            }
        } else {
            queryWrapper.select();
        }
        if (departmentId != null) {
            queryWrapper.eq("department_id", departmentId);
        }
        if (name != null && !name.equals("")) {
            queryWrapper.like("name", name);
        }
        if (year != null && year != 0) {
            queryWrapper.eq("year", year);
        }
        if (modelType != null) {
            queryWrapper.eq("model_type", modelType);
        }
        if (breakdowns != null && !breakdowns.equals("")) {
            queryWrapper.like("breakdowns", breakdowns);
        }
        queryWrapper.orderByDesc("id");
        Page<TestsModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<TestsModel> roleList = testsMapper.selectPage(pages, queryWrapper);
        return roleList;
    }

    @Override
    public void update(TestsModel model) {
        testsMapper.updateById(model);
    }

    @Override
    public void insert(TestsModel model) {
        testsMapper.insert(model);
    }

    @Override
    public void delete(int id) {
        testsMapper.deleteById(id);
    }

    @Override
    public Integer testCount(List<Integer> idsList) {
        QueryWrapper<TestsModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        if (idsList != null && idsList.size() > 0) {
            queryWrapper.in("department_id", idsList);
        }
        Integer testCount = testsMapper.selectCount(queryWrapper);
        return testCount;
    }

    @Override
    public List<TestsModel> getStatisticsByIds(List<Integer> idList, List<Integer> yearList, String key) {
        QueryWrapper<TestsModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(key, "COUNT(*) as count");
        if (idList.size() != 0) {
            queryWrapper.in("department_id", idList);
            queryWrapper.in("year", yearList);
        } else {
            queryWrapper.in("year", yearList);
        }
        queryWrapper.groupBy(key);
        List<TestsModel> testsModelList = testsMapper.selectList(queryWrapper);
        return testsModelList;

    }

    @Override
    public List<TestsModel> getTestByIds(List<Integer> idList, String key, Integer year) {
        QueryWrapper<TestsModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("model_type", key, "COUNT(*) as count");
        queryWrapper.eq("year", year);
        if (idList != null && idList.size() > 0) {
            queryWrapper.in("model_type", idList);
            queryWrapper.groupBy(key);
            List<TestsModel> testsModelList = testsMapper.selectList(queryWrapper);
            return testsModelList;
        } else {
            return null;
        }
    }

    @Override
    public List<TestsModel> getTestByType(Integer modelType) {
        QueryWrapper<TestsModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("model_type", modelType);
        List<TestsModel> testsModelList = testsMapper.selectList(queryWrapper);
        return testsModelList;
    }

    @Override
    public List<TestsModel> getPlantByIds(List<Integer> appIdList) {
        QueryWrapper<TestsModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().in("id", appIdList);
        List<TestsModel> personModelList = testsMapper.selectList(queryWrapper);
        return personModelList;
    }

    /**
     * 批量删除实验数据
     * @param ids
     * @return
     */
    @Override
    public Result<Object> deleteBatchByIds(String ids) {
        // 参数校验
        if (StringUtils.isEmpty(ids)) {
            return Result.success("参数为空，无需删除");
        }
        List<Integer> idsList = Arrays.stream(ids.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());
        // 批量删除
        if (!ListUtils.isEmpty(idsList)) {
            testsMapper.deleteBatchIds(idsList);
            return Result.success("批量删除成功");
        } else {
            return Result.success("批量删除失败，数据为空");
        }
    }
}
